<?php
$script = "
$('#url-container').duplicate({
    item_id_prefix: 'url-',
    item_class : 'url'
});
//add
$('#url-container').delegate('.url input:last', 'blur', function(){
    var parent = $(this).parents('.url').first();
    var add_new = true;
    $.each(\$('input', parent), function(){
        if (\$(this).val() == '') {
            add_new = false;
            return false;
        }
    });
    if (add_new)
        $.fn.duplicate.add('url-container', parent);
});
//remove
$('#url-container').delegate('.url input', 'blur', function(){
    var parent = $(this).parents('.url').first();
    var remove = true;
    $.each(\$('input', parent), function(){
        if (\$(this).val() != '') {
            remove = false;
            return false;
        }
    });
    if (parent.attr('id') == $('#url-container .url:last').attr('id'))
        remove = false;
    if (remove)
        $.fn.duplicate.remove('url-container', parent);
});
";
Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/jquery.duplicate.js');
Yii::app()->clientScript->registerScript(__CLASS__.'#DuplicateUrl', $script, CClientScript::POS_READY);
?>
<?php foreach ($models as $i => $model):?>
    <div id="url-<?php echo $i;?>" class="url">
        <div class="row">
            <div class="textbox">
            <span>
            <?php echo $form->textField($model,'['.$i.']url', array('size'=>60)); ?>
            </span>
            </div>
        </div>
    </div>
<?php endforeach;?>